<?php 
	include("includes/connect_db.php"); 

	$idFormat = $_POST['id'];
	$idKontak = $_POST['idKontak'];
	$tipe = $_POST['tipe'];
	$act = $_GET['act'];

	switch ($act) {
		case '1':
			if ($tipe == 'mahasiswa') {
				$sql = "SELECT * FROM tb_mhs WHERE 
					telepon <> '' AND 
					telepon NOT LIKE '%/%' AND 
					telepon NOT LIKE '%-%' AND 
					telepon NOT LIKE '%,%' AND 
					telepon NOT LIKE '% %' AND 
					telepon NOT LIKE '%(%' AND 
					telepon NOT LIKE '%.%' AND 
					telepon NOT REGEXP '[abc]'
					ORDER BY nim";
				$query = mysql_query($sql);
				while ($result = mysql_fetch_array($query)) {
					$sqlSelect = "SELECT * FROM tb_privilege 
									WHERE id_kontak = '$result[nim]' AND id_operasi = $idFormat AND tipe = '$tipe'";
					$querySelect = mysql_query($sqlSelect);
					$jumlah = mysql_num_rows($querySelect);

					if ($jumlah == 0) {
						$sqlInsert = "INSERT INTO tb_privilege VALUES ('$result[nim]', '$idFormat', '$tipe')";
						$queryInsert = mysql_query($sqlInsert);
					}
				}
			} elseif ($tipe == 'dosen') {
				$sql = "SELECT * FROM tb_master_pegawai WHERE 
					telp <> '' AND 
					telp NOT LIKE '%/%' AND 
					telp NOT LIKE '%-%' AND 
					telp NOT LIKE '%,%' AND 
					telp NOT LIKE '% %' AND 
					telp NOT LIKE '%(%' AND 
					telp NOT LIKE '%.%' AND 
					telp NOT REGEXP '[abc]'
					ORDER BY nip_baru";
				$query = mysql_query($sql);
				while ($result = mysql_fetch_array($query)) {
					$sqlSelect = "SELECT * FROM tb_privilege 
									WHERE id_kontak = '$result[nip_baru]' AND id_operasi = $idFormat AND tipe = '$tipe'";
					$querySelect = mysql_query($sqlSelect);
					$jumlah = mysql_num_rows($querySelect);

					if ($jumlah == 0) {
						$sqlInsert = "INSERT INTO tb_privilege VALUES ('$result[nip_baru]', '$idFormat', '$tipe')";
						$queryInsert = mysql_query($sqlInsert);
					}
				}
			} elseif ($tipe == 'kontak') {
				$sql = "SELECT * FROM tb_kontak";
				$query = mysql_query($sql);
				while ($result = mysql_fetch_array($query)) {
					$sqlSelect = "SELECT * FROM tb_privilege 
									WHERE id_kontak = '$result[id_kontak]' AND id_operasi = $idFormat AND tipe = '$tipe'";
					$querySelect = mysql_query($sqlSelect);
					$jumlah = mysql_num_rows($querySelect);

					if ($jumlah == 0) {
						$sqlInsert = "INSERT INTO tb_privilege VALUES ('$result[id_kontak]', '$idFormat', '$tipe')";
						$queryInsert = mysql_query($sqlInsert);
					}
				}
			}
			break;


		case '2':
			$sql = "DELETE FROM tb_privilege WHERE id_operasi = '$idFormat' AND tipe = '$tipe'";
			$query = mysql_query($sql);
			break;
		


		case '3':
			$sql = "INSERT INTO tb_privilege VALUES ('$idKontak', '$idFormat', '$tipe')";
			$query = mysql_query($sql);
			break;


		case '4':
			$sql = "DELETE FROM tb_privilege WHERE id_kontak = '$idKontak' AND id_operasi = '$idFormat' AND tipe = '$tipe'";
			$query = mysql_query($sql);
			break;
	}
?>